/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package gui;
import app.ConsultaCartelera;
import javax.microedition.lcdui.*;

/**
 *
 * @author REX
 */
public class FormularioCompra extends Form implements CommandListener {
    private TextField boletos = new TextField("Numero De Boletos: ", "", 3, TextField.NUMERIC);
    private MenuCartelera mc;
    private ConsultaCartelera middlet;
    private Pelicula p;
    public FormularioCompra(String label, Pelicula p, MenuCartelera mc) {
        super(label);
        this.mc = mc;
        this.p = p;
        append("Pelicula : "+p.getTitulo()+"\n");
        append("Horario : "+p.getHora()+"\n");
        append("Costo por Boleto : "+p.getCostopb()+"\n");
        append(boletos);
        setCommandListener(this);
        addCommand(new Command("Comprar", Command.SCREEN, 1));
	addCommand(new Command("Regresar", Command.BACK, 2));
        middlet =  mc.getMc().getMp().getMidlet();

    }
    public void commandAction(Command command, Displayable d) {
        if(command.getLabel().equals("Regresar")){
            middlet.pantalla.setCurrent(mc);
        }if(command.getLabel().equals("Comprar")){
            String cantidad = boletos.getString();
            if(cantidad.equals("")){
                middlet.pantalla.setCurrent(new Alert("No has ingresado los boletos"), d);
                return;
            }
             if( Integer.parseInt(cantidad) <= 0 ){
                middlet.pantalla.setCurrent(new Alert("Cantidad no valida"), d);
                return;
            }
            int saldo = middlet.saldo;
            int costo = saldo - p.getCostopb()*Integer.parseInt(boletos.getString());
           
            if(costo >0){
                middlet.saldo = costo;
                middlet.pantalla.setCurrent(new Compra(this));
                System.out.println("Saldo :"+middlet.saldo );
            }else{
                middlet.pantalla.setCurrent(new Alert("No tienes saldo suficiente"), this);
            }
           

        }
    }

    public TextField getBoletos() {
        return boletos;
    }

    public void setBoletos(TextField boletos) {
        this.boletos = boletos;
    }

    public MenuCartelera getMc() {
        return mc;
    }

    public void setMc(MenuCartelera mc) {
        this.mc = mc;
    }
    
    

   


}
